package com.it.ems.service.impl;

import com.it.ems.entity.ExpressOrder;
import com.it.ems.mapper.ExpressOrderMapper;
import com.it.ems.service.ExpressOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.HashMap;
import java.util.Map;

@Service
public class ExpressOrderServiceImpl implements ExpressOrderService {
    
    @Autowired
    private ExpressOrderMapper expressOrderMapper;

    // 定义状态映射：英文 -> 中文
    private static final Map<String, String> STATUS_TO_CHINESE = new HashMap<>();
    // 定义状态映射：中文 -> 英文
    private static final Map<String, String> CHINESE_TO_STATUS = new HashMap<>();

    static {
        STATUS_TO_CHINESE.put("PENDING", "待处理");
        STATUS_TO_CHINESE.put("SHIPPED", "已发货");
        STATUS_TO_CHINESE.put("PICKED_UP", "已揽收");
        STATUS_TO_CHINESE.put("IN_TRANSIT", "运输中");
        STATUS_TO_CHINESE.put("UNRECEIVED", "未签收");
        STATUS_TO_CHINESE.put("DELIVERED", "已签收");
        STATUS_TO_CHINESE.put("CANCELED", "已取消");

        CHINESE_TO_STATUS.put("待处理", "PENDING");
        CHINESE_TO_STATUS.put("已发货", "SHIPPED");
        CHINESE_TO_STATUS.put("已揽收", "PICKED_UP");
        CHINESE_TO_STATUS.put("运输中", "IN_TRANSIT");
        CHINESE_TO_STATUS.put("未签收", "UNRECEIVED");
        CHINESE_TO_STATUS.put("已签收", "DELIVERED");
        CHINESE_TO_STATUS.put("已取消", "CANCELED");
    }

    private String toChineseStatus(String status) {
        return STATUS_TO_CHINESE.getOrDefault(status, status); // 如果没有映射，返回原值
    }

    private String toEnglishStatus(String chineseStatus) {
        return CHINESE_TO_STATUS.getOrDefault(chineseStatus, chineseStatus); // 如果没有映射，返回原值
    }
    
    @Override
    public ExpressOrder findById(Long id) {
        ExpressOrder order = expressOrderMapper.findById(id);
        if (order != null) {
            order.setStatus(toEnglishStatus(order.getStatus()));
        }
        return order;
    }
    
    @Override
    public List<ExpressOrder> findByUserId(Long userId) {
        List<ExpressOrder> orders = expressOrderMapper.findByUserId(userId);
        orders.forEach(order -> order.setStatus(toEnglishStatus(order.getStatus())));
        return orders;
    }
    
    @Override
    public List<ExpressOrder> findByOrderNumber(String orderNumber) {
        List<ExpressOrder> orders = expressOrderMapper.findByOrderNumber(orderNumber);
        orders.forEach(order -> order.setStatus(toEnglishStatus(order.getStatus())));
        return orders;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(ExpressOrder order) {
        order.setStatus(toChineseStatus(order.getStatus())); // 转换为中文存储
        return expressOrderMapper.insert(order) > 0;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(ExpressOrder order) {
        order.setStatus(toChineseStatus(order.getStatus())); // 转换为中文存储
        return expressOrderMapper.update(order) > 0;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return expressOrderMapper.delete(id) > 0;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByUserId(Long userId) {
        return expressOrderMapper.deleteByUserId(userId) > 0;
    }

    @Override
    public int countTodayOrders() {
        return expressOrderMapper.countTodayOrders();
    }

    @Override
    public int countOrdersByStatus(String status) {
        // 在这里也需要转换状态，因为数据库是中文状态
        return expressOrderMapper.countOrdersByStatus(toChineseStatus(status));
    }

    @Override
    public List<ExpressOrder> findRecentOrders(int limit) {
        List<ExpressOrder> orders = expressOrderMapper.findRecentOrders(limit);
        orders.forEach(order -> order.setStatus(toEnglishStatus(order.getStatus())));
        return orders;
    }

    @Override
    public List<ExpressOrder> findAllOrders() {
        List<ExpressOrder> orders = expressOrderMapper.findAllOrders();
        orders.forEach(order -> order.setStatus(toEnglishStatus(order.getStatus())));
        return orders;
    }

    @Override
    public List<ExpressOrder> findReceivedOrdersByUserId(Long userId) {
        List<ExpressOrder> orders = expressOrderMapper.findReceivedOrdersByUserId(userId);
        orders.forEach(order -> order.setStatus(toEnglishStatus(order.getStatus())));
        return orders;
    }

    @Override
    public List<ExpressOrder> findSentOrdersByUserId(Long userId) {
        List<ExpressOrder> orders = expressOrderMapper.findSentOrdersByUserId(userId);
        orders.forEach(order -> order.setStatus(toEnglishStatus(order.getStatus())));
        return orders;
    }
} 